//
//  XHRNavigationController.m
//  BuDeJie
//
//  Created by haoran on 16/4/2.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHRNavigationController.h"
@interface  XHRNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation XHRNavigationController

// 全屏滑动: 分析:为什么导航控制器的滑动手势只能边缘滑动

/*
 UIScreenEdgePanGestureRecognizer
 滑动手势
 // 触发UIScreenEdgePanGestureRecognizer就会有滑动功能
 // 触发UIScreenEdgePanGestureRecognizer就会调用它的target的action
 // UIScreenEdgePanGestureRecognizer的targer的action => 滑动返回功能
 
 <UIScreenEdgePanGestureRecognizer: 0x7fc310eb72c0; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fc310eb5810>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fc310eb75e0>)>>
 
 */

// target=<_UINavigationInteractiveTransition 0x7fc310eb75e0>)

//  <_UINavigationInteractiveTransition: 0x7f9c7234d010>:滑动手势代理


-(void)viewDidLoad{
    
    [super viewDidLoad];
    
    XHRLog(@"%@",self.interactivePopGestureRecognizer.delegate);
    //滑动功能
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    [self.view addGestureRecognizer:pan];
    
    //控制器什么时候触发
    pan.delegate  = self;
    // Bug:假死状态:程序一直在跑,但是界面死了
    
    // 在根控制器下,滑动返回, 不应该在跟控制器的view上滑动返回
    
    // 清空手势代理,恢复滑动返回功能
    self.interactivePopGestureRecognizer.enabled = NO;
//    self.interactivePopGestureRecognizer.delegate = self;

}

+(void)load{

    //获取全局的
    // 只要遵守了UIAppearance,就可以调用appearance 获取全局外观
    //    UINavigationBar *navBar = [UINavigationBar appearance]; 不建议使用,否则很可能会引起其他导航条的bug,比如老师上课讲的短信界面
    
    //获取当前类下的导航条
    UINavigationBar * navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    //设置标题
    //设置导航条标题字体
    [UINavigationBar appearance];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
//    navBar.titleTextAttributes = dict; 也可以
    [navBar setTitleTextAttributes:dict];

    //设置背景图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    //必须是UIBarMetricsDefault模式
    
    
    
}


//重写push方法,设置全局的返回按钮
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    // 导航控制器默认有一个滑动返回功能
    
    // 把系统返回按钮覆盖,就没有滑动返回功能 ->
    
    // 滑动返回功能为什么失效: 用了滑动手势去做 , 验证:滑动手势在不在(pass)
    // 代理可以控制手势是否有效,验证:代理做了一些事情,导致滑动手势失效
    if(self.childViewControllers.count){
        
    //隐藏底部tabBar
    viewController.hidesBottomBarWhenPushed = YES;
    
    //非根控制器才需要设置返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
    
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [backBtn sizeToFit];
    //注意:一定要在按钮内容有尺寸的时候,设置才有效果
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
    
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        
    //包装成一个View 控制返回按钮的点击范围
    UIView *view = [[UIView alloc]initWithFrame:backBtn.frame];
    [view addSubview:backBtn];
        
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
    }

    //这个方法才是真正执行跳转
    [super pushViewController:viewController animated:YES];
}

//实现back方法,回到上一个控制器
-(void)back{

    [self popViewControllerAnimated:YES];
}

#pragma mark ---- UIGestureRecognizerDelegate
//是否触发手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    // 在根控制器下 不要 触发手势
    return self.childViewControllers.count > 1;
}


@end
